from app import create_app, db
from app.models import MonitorPoint, MonitorData
from datetime import datetime, timedelta
import random

def create_test_data():
    app = create_app()
    with app.app_context():
        # 清除现有的测试数据
        MonitorData.query.delete()
        MonitorPoint.query.delete()
        
        # 创建测试监控点
        test_points = [
            {
                'name': '1号养殖池',
                'location': 'A区-东侧',
                'description': '草鱼养殖池'
            },
            {
                'name': '2号养殖池',
                'location': 'A区-西侧',
                'description': '鲫鱼养殖池'
            },
            {
                'name': '3号养殖池',
                'location': 'B区-北侧',
                'description': '鲤鱼养殖池'
            }
        ]
        
        created_points = []
        for point_data in test_points:
            point = MonitorPoint(**point_data)
            db.session.add(point)
            created_points.append(point)
        
        db.session.commit()
        
        # 为每个监控点生成24小时的测试数据
        now = datetime.utcnow()
        for point in created_points:
            # 生成过去24小时的数据，每小时一条
            for hours_ago in range(24, -1, -1):
                timestamp = now - timedelta(hours=hours_ago)
                
                # 生成合理范围内的随机数据
                data = MonitorData(
                    point_id=point.id,
                    dissolved_oxygen=round(random.uniform(4.0, 8.0), 2),  # 溶解氧 4-8 mg/L
                    ph_value=round(random.uniform(6.5, 8.5), 2),         # pH值 6.5-8.5
                    temperature=round(random.uniform(20.0, 28.0), 1),     # 温度 20-28 ℃
                    ammonia_nitrogen=round(random.uniform(0.1, 0.5), 2),  # 氨氮 0.1-0.5 mg/L
                    turbidity=round(random.uniform(10.0, 30.0), 2),       # 浊度 10-30 NTU
                    timestamp=timestamp
                )
                db.session.add(data)
        
        db.session.commit()
        print('测试数据创建成功！')
        
        # 打印统计信息
        points_count = MonitorPoint.query.count()
        data_count = MonitorData.query.count()
        print(f'已创建 {points_count} 个监控点')
        print(f'已创建 {data_count} 条监控数据')

if __name__ == '__main__':
    create_test_data() 